﻿CREATE PROCEDURE [dbo].[UserRestore_GetEmail]
	@restoreHashCode NVARCHAR(32)
AS
BEGIN
/*
SELECT [Email]
      ,[UserHashCode]
      ,[RestoreHashCode]
      ,[CreateDate]
  FROM [dbo].[UserRestore]
GO


*/
	IF EXISTS (SELECT * FROM [dbo].[user_Restore] WHERE [RestoreHashCode] = @restoreHashCode)
	BEGIN
		DECLARE @email NVARCHAR(256)
		DECLARE @userHashCode  NVARCHAR(32)
		SELECT @email = [Email], @userHashCode = [UserHashCode] FROM [dbo].[user_Restore] WHERE [RestoreHashCode] = @restoreHashCode

		IF (SELECT COUNT(*) FROM [dbo].[user_User] WHERE [Email] = @email AND [HashCode] = @userHashCode) > 0
		BEGIN
			SELECT @email AS 'Email'
		END
		ELSE
		BEGIN
			RAISERROR ('RestoreHashCodeExpired',9,1)
			RETURN;
		END
	END
	ELSE
	BEGIN
		RAISERROR ('UnknownRestoreHashCode',9,1)
		RETURN;
	END

END